package com.ruoyi.health.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.health.domain.Favorite;
import com.ruoyi.health.domain.vo.FavoriteVo;
import com.ruoyi.health.mapper.FavoriteMapper;
import com.ruoyi.health.service.IFavoriteService;
import com.ruoyi.health.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteServiceImpl implements IFavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public int insertFavorite(Favorite favorite) {
        //不让重复收藏同一个服务
        Long favoriteId = favoriteMapper.selectFavoriteIdByUserIdAndServiceId(favorite);
        if(StringUtils.isNotNull(favoriteId)){
            throw new ServiceException("该服务已收藏，不允许重复操作！");
        }
        //新增收藏
        return favoriteMapper.insertFavorite(favorite);
    }

    @Override
    public int deleteFavorite(Favorite favorite) {
        //只能取消自己所收藏的养老服务
        //先查询用户收藏养老服务的favorite表Id
        Long favoriteId = favoriteMapper.selectFavoriteIdByUserIdAndServiceId(favorite);
        if(StringUtils.isNotNull(favoriteId)){
            return favoriteMapper.deleteFavorite(favoriteId);
        }
        throw new ServiceException("取消收藏失败！");
    }

    @Override
    public List<FavoriteVo> selectFavoriteWithServiceByUserId(Long userId) {
        List<FavoriteVo> favoriteVoList = favoriteMapper.selectFavoriteWithServiceByUserId(userId);
        return favoriteVoList;
    }

    @Override
    public int getFavoriteStatus(Favorite favorite) {
        Long favoriteId = favoriteMapper.selectFavoriteIdByUserIdAndServiceId(favorite);
        if(StringUtils.isNotNull(favoriteId)){
            return 1;  //已收藏
        }
        return 0;   //未收藏
    }


}
